package com.p2p.backend.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.backend.service.BackBankCardService;
import com.p2p.backend.service.OplogService;
import com.p2p.backend.util.PageUtils;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@RequestMapping("bankcard")
@Controller
public class BackBankCardController extends BaseController {

	@Autowired
	private BackBankCardService bankCardService;

	@PermissionNeed("bankcard_list")
	@RequestMapping("list")
	public void list(PageVo form, Model model) {
		form.handleSearch();
		PageVo page = bankCardService.listPage(form);
		form.resetSearch();
		model.addAttribute("res", 1);
		model.addAttribute("data", PageUtils.data(page));
	}

	@PermissionNeed("bankcard_unpass")
	@RequestMapping("unpass")
	public void unpass(HttpServletRequest request, @RequestParam Integer id,
			ModelMap model) {
		bankCardService.unpass(id);
		model.addAttribute("res", 1);
		unpassOplog(request, id);
	}

	@PermissionNeed("bankcard_pass")
	@RequestMapping("pass")
	public void pass(HttpServletRequest request, @RequestParam Integer id,
			Model model) {
		bankCardService.pass(id);
		model.addAttribute("res", 1);
		passOplog(request, id);
	}

	private void unpassOplog(HttpServletRequest request, Integer id) {
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK_CARD);
		oplog.setAction(Action.AUDIT_PASS);
		String params = "id=" + id;
		oplog.setParams(params);
		OplogService.log(oplog);
	}

	private void passOplog(HttpServletRequest request, Integer id) {
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK_CARD);
		oplog.setAction(Action.AUDIT_PASS);
		String params = "id=" + id;
		oplog.setParams(params);
		OplogService.log(oplog);
	}

}
